﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.teacher
{
    public partial class view_submission : System.Web.UI.Page
    {
        int submission_id;

        protected void Page_Load(object sender, EventArgs e)
        {

            Label7.Visible = false;
            submission_id = Convert.ToInt32(Request.QueryString["id"]);
            if (!Page.IsPostBack)
            {
                populatePage();
            }
        }

        private void populatePage() 
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var info = (from s in db.submissions
                              join a in db.assignments on s.assignment_id equals a.assignment_id
                              join ui in db.user_info on s.user_id equals ui.user_id
                              join f in db.files on s.file_id equals f.file_id
                              where s.submission_id == submission_id
                              select new
                              {
                                  student_name = ui.user_info_first_name + " " + ui.user_info_last_name,
                                  assignment_name = a.assignment_title,
                                  date = s.submission_date,
                                  deadline = a.assignment_deadline,
                                  file_id = f.file_id,
                                  file_title = f.file_name,
                                  file_size = f.file_size,
                                  file_type = f.file_type,
                                  file_content = f.file_content

                              }).SingleOrDefault();

            Label1.Text = info.assignment_name;
            Label2.Text = info.deadline.ToString();
            Label3.Text = info.student_name;
            Label4.Text = info.date.ToString();
            Label5.Text = "<a href='../downloadFile.aspx?id=" + info.file_id + "'>" + info.file_title + "</a>";
            Label6.Text = "Ikki rættað";

            var csOrg = (from cs1 in db.corrected_submission
                         where cs1.submission_id == submission_id
                         select cs1).SingleOrDefault();

            if (csOrg != null)
            {
                int approved = csOrg.corrected_submission_approved;
                String comment = csOrg.corrected_submission_comment;
                String grade = csOrg.corrected_submission_grade;
                if (approved == 1)
                {
                    CheckBox1.Checked = true;
                }
                TextBox1.Text = comment;
                TextBox2.Text = grade;
                Label6.Text = csOrg.corrected_date.ToString();
            }
        }

        private void addCorrectedSubmission()
        {
            Boolean c = CheckBox1.Checked;
            int approved = 0;
            if(c) {
                approved = 1;
            }
            String comment = TextBox1.Text;
            String grade = TextBox2.Text;

            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var csOrg = (from cs1 in db.corrected_submission
                         where cs1.submission_id == submission_id
                         select cs1).SingleOrDefault();

            var submission = (from sub in db.submissions
                              join a in db.assignments on sub.assignment_id equals a.assignment_id
                              where sub.submission_id == submission_id
                              select new
                              {
                                  assignment_deadline = a.assignment_deadline
                              }).SingleOrDefault();

            if (submission.assignment_deadline <= DateTime.Now)
            {

                if (csOrg != null)
                {
                    db.corrected_submission.DeleteObject(csOrg);
                    db.SaveChanges();
                }

                corrected_submission cs = new corrected_submission()
                {
                    corrected_submission_approved = approved,
                    corrected_submission_comment = comment,
                    corrected_submission_grade = grade,
                    submission_id = submission_id,
                    corrected_date = DateTime.Now
                };

                db.corrected_submission.AddObject(cs);
                db.SaveChanges();

                Response.Redirect("view_assignment.aspx");
            }
            else
            {
                error("Freistin er ikki farin enn");
            }
        }

        private void error(String s)
        {
            Label7.Text = "<tr><td colspan='2' align='center'><span style='color:red;'>"+s+"<span></td></tr>";
            Label7.Visible = true;
            Label7.ForeColor = System.Drawing.Color.Red;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("view_assignment.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            addCorrectedSubmission();
        }
    }
}